<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 字典类型列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button> -->
                <el-input v-model="query.name" placeholder="类型名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="GetDictList">搜索</el-button>
                <el-button type="primary" icon="el-icon-search" @click="AddDictType">添加</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="dict_id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="dict_type" label="字典类型名称" width="180" align="center"></el-table-column>
                <el-table-column prop="remark" label="备注说明" width="180" align="center"></el-table-column>
                <el-table-column prop="updateName" label="修改人" width="180" align="center"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间" width="180" align="center"></el-table-column>
                <el-table-column prop="status" label="状态" width="180" align="center">
                    <template slot-scope="aaa">
                        <el-tag type="success" v-if="aaa.row.status == 0">启用</el-tag>
                        <el-tag type="danger" v-if="aaa.row.status == 1">停用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageindex"
                    :page-size="query.pagesize"
                    :total="total"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="字典类型名称">
                    <el-input v-model="form.dict_type"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark"></el-input>
                </el-form-item>
                <el-form-item label="字典状态">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="0">启用</el-radio>
                        <el-radio :label="1">停用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getDictTypeList, GetDictTypeById, PutDictTypeInfo } from '@/api/dictionary.js/';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                name: '',
                pageindex: 1,
                pagesize: 3
            },
            tableData: [],
            multipleSelection: [],
            editVisible: false,
            total: 0,
            form: {
                dict_type: '',
                remark: '',
                status: undefined
            }
        };
    },
    created() {
        this.GetDictList();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        GetDictList() {
            getDictTypeList(this.query).then((res) => {
                this.tableData = res.data.plist;
                this.total = res.data.recordCount;
            });
        },
        // 删除操作
        handleDelete(index, row) {},
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 编辑操作
        handleEdit(index, row) {
            this.editVisible = true;
            let ids = {
                id: row.dict_id
            };
            GetDictTypeById(ids).then((res) => {
                this.form = res;
            });
        },
        // 保存编辑
        saveEdit() {
            PutDictTypeInfo(this.form).then((res) => {
                if (res.code == 200) {
                    this.$message.success(res.message);
                } else {
                    this.$message({
                        showClose: true,
                        message: '修改失败！',
                        type: 'error'
                    });
                }
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageindex', val);
            this.GetDictList();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
